package ap.project.joystick;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Handler;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import ap.project.R;

public class GameSurface extends SurfaceView implements SurfaceHolder.Callback {

	private Context _context;
	private GameThread _thread;
	private GameControls _controls;

	private GameJoystick _joystick;

	private Bitmap _pointer;

	private int prevX;
	private int prevY;
	private int prevIsShooting;

	Socket mainsock;

	public GameSurface(Context context, Socket sock) {
		super(context);
		// TODO Auto-generated constructor stub
		_context = context;
		mainsock = sock;
		init();
	}

	private void send(int x, int y, int isShooting) {

		PrintWriter outp = null;
		BufferedReader inp = null;
		String serverMsg = null;

		try {
			outp = new PrintWriter(mainsock.getOutputStream(), true);
			inp = new BufferedReader(new InputStreamReader(
					mainsock.getInputStream()));
			// serverMsg = inp.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			// Zend mijn gegevens
			// Log.d(Constants.TAG, "Ik verstuur: \t" + x + ", +" + y + "\n");
			outp.println((x - 425) + "," + (y - 267) +","+isShooting);

			// Lees de server gegevens
			serverMsg = inp.readLine();
			// Log.d(Constants.TAG, "Ik ontvang: \t" + serverMsg + "\n \n");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private void init() {
		// initialize our screen holder
		SurfaceHolder holder = getHolder();
		holder.addCallback(this);

		// initialize our game engine

		// initialize our Thread class. A call will be made to start it later
		_thread = new GameThread(holder, _context, new Handler(), this);
		setFocusable(true);

		_joystick = new GameJoystick(getContext().getResources());
		_pointer = (Bitmap) BitmapFactory.decodeResource(getResources(),
				R.drawable.icon);
		// contols
		_controls = new GameControls();
		setOnTouchListener(_controls);
	}

	public void doDraw(Canvas canvas) {

		// update the pointer
		_controls.update(null);

		// Check if data to be send is not already sent.
		if (prevX != _controls._touchingPoint.x
				|| prevY != _controls._touchingPoint.y || prevIsShooting != _controls.isShooting) {
			send(_controls._touchingPoint.x, _controls._touchingPoint.y, _controls.isShooting);
		}
		// draw the pointer
		canvas.drawBitmap(_pointer, _controls._pointerPosition.x,
				_controls._pointerPosition.y, null);

		// draw the joystick background
		canvas.drawBitmap(_joystick.get_joystickBg(), 380, 220, null);

		// draw the dragable joystick
		canvas.drawBitmap(_joystick.get_joystick(),
				_controls._touchingPoint.x - 26,
				_controls._touchingPoint.y - 26, null);

		prevX = _controls._touchingPoint.x; // set for check during next call
		prevY = _controls._touchingPoint.y;
		prevIsShooting = _controls.isShooting;
		
		canvas.drawBitmap(_joystick.get_shootbutton(), 0, 0, null);
		

	}

	// these methods are overridden from the SurfaceView super class. They are
	// automatically called
	// when a SurfaceView is created, resumed or suspended.
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
	}

	private boolean retry;

	public void surfaceDestroyed(SurfaceHolder arg0) {
		retry = true;
		// code to end gameloop
		_thread.state = GameThread.STOPED;
		while (retry) {
			try {
				// code to kill Thread
				_thread.join();
				retry = false;
			} catch (InterruptedException e) {
			}
		}

	}

	public void surfaceCreated(SurfaceHolder arg0) {
		if (_thread.state == GameThread.PAUSED) {
			// When game is opened again in the Android OS
			_thread = new GameThread(getHolder(), _context, new Handler(), this);
			_thread.start();
		} else {
			// creating the game Thread for the first time
			_thread.start();
		}
	}

	public void Update() {
		// TODO Auto-generated method stub

	}

}
